package com.kiloai.digcust.mvp.presenter;

import com.kiloai.base.basemvp.BasePresenter;
import com.kiloai.base.entity.Code;
import com.kiloai.base.entity.User;
import com.kiloai.base.okgohttp.callback.RequestCallback;
import com.kiloai.base.utils.ToastUtils;
import com.kiloai.digcust.R;
import com.kiloai.digcust.mvp.model.CodeModel;
import com.kiloai.digcust.mvp.model.PersonalModel;
import com.kiloai.digcust.mvp.view.activity.LoginActivity;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

/**
 * Created by david on 2018/8/15.
 */

public class LoginPresenter extends BasePresenter<LoginActivity> {

    public void getCode(String team, String name) {
        CodeModel.getCode(getView(), team, name, new RequestCallback<Code>() {
            @Override
            public void onSuccess(Code code) {
                if (code != null) {

                    timeDownCount();
                }
                ToastUtils.show(getView(), "短信发送成功！");
            }
        });
    }

    public void login(String team, String name, String pwd, String code) {
        Map<String,String> data = new HashMap<>();
        data.put("team",team);
        data.put("name",name);
        data.put("pwd",pwd);
        data.put("captcha",code);
        PersonalModel.login(getView(), data, new RequestCallback<User>() {
            @Override
            public void onSuccess(User user) {
                if (user != null){

                    getView().loginSuccess();
                }
            }
        });

    }

    // 倒计时
    private void timeDownCount() {
        final int count = 119;
        Observable.interval(0, 1, TimeUnit.SECONDS)//Interval — 创建一个定时发射整数序列的Observable
                .take(count)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(@NonNull Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .compose(getView().<Long>bindToLifecycle())//使用RxLifecycle来管理observable生命周期
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long aLong) {
                        if (getView() != null) {
                            getView().setGetCodeView(aLong + getView().getString(R.string.minutes_letter), false);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        if (getView() != null) {
                            getView().setGetCodeView(getView().getString(R.string.code_get), true);
                        }
                    }
                });
    }
}
